/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import user from "./user";

//create
function create_Test() { 
    let expectedValue = {"sessionId":"testSynchronouslyLoadProtoFile","userPrivilege":"John123","isTokenType":false,"formatTimestamp":"12342222"};
    let msg;
    let startTime = Date.now();
    for (let i = 0; i < 100000; i++) {
        msg = user.UserLoginResponse.create({
            sessionId: "testSynchronouslyLoadProtoFile",
            userPrivilege: "John123",
            isTokenType: false,
            formatTimestamp: "12342222"
        });
    }
    let endTime = Date.now();
    let arrayBuffer = user.UserLoginResponse.encode(msg).finish();
    // print("protobuf arrayBuffer:" + arrayBuffer);
    // print("序列化数据：" + JSON.stringify(arrayBuffer) + "\r\n");

    let decodeMsg = user.UserLoginResponse.decode(arrayBuffer);
    // print("protobuf decode:" + JSON.stringify(decodeMsg));
    // print("反序列化数据：" + JSON.stringify(decodeMsg));
    if (!areEqual(JSON.stringify(decodeMsg), JSON.stringify(expectedValue))) {
        throw new Error('create failed');
    }
    print(`protobuf_create: ${endTime - startTime} ms`);
}


//encode
function encode_Test() {
    let expectedValue = [10,30,116,101,115,116,83,121,110,99,104,114,111,110,111,117,115,108,121,76,111,97,100,80,114,111,116,111,70,105,108,101,18,7,74,111,104,110,49,50,51,24,0,34,8,49,50,51,52,50,50,50,50];
    let arrayBuffer;
    let msg = user.UserLoginResponse.create({
        sessionId: "testSynchronouslyLoadProtoFile",
        userPrivilege: "John123",
        isTokenType: false,
        formatTimestamp: "12342222"
    });
    let startTime = Date.now();
    for (let i = 0; i < 10000; i++) {
        arrayBuffer = user.UserLoginResponse.encode(msg).finish();
    }
    let endTime = Date.now();
    if (!areEqual(arrayBuffer.toString(), expectedValue.toString())) {
    throw new Error('encode failed');
    }
    
    print(`protobuf_encode: ${endTime - startTime} ms`);
}


//encodeDelimited
function encodeDelimited_Test() {
    let msg = user.UserLoginResponse.create({
        sessionId: "testSynchronouslyLoadProtoFile",
        userPrivilege: "John123",
        isTokenType: false,
        formatTimestamp: "12342222"
    });
    let arrayBuffer;
    let expectedValue = [53,10,30,116,101,115,116,83,121,110,99,104,114,111,110,111,117,115,108,121,76,111,97,100,80,114,111,116,111,70,105,108,101,18,7,74,111,104,110,49,50,51,24,0,34,8,49,50,51,52,50,50,50,50];
    let startTime = Date.now();
    for (let i = 0; i < 10000; i++) {
        arrayBuffer = user.UserLoginResponse.encodeDelimited(msg).finish();
        // print("protobuf arrayBuffer:" + arrayBuffer);
    }
    let endTime = Date.now();
    if (!areEqual(arrayBuffer.toString(), expectedValue.toString())) {
        throw new Error('encodeDelimited failed');
    }
    print(`protobuf_encodeDelimited: ${endTime - startTime} ms`);
}


//decode(arrayBuffer)
function decode_Test() {
    let msg = user.UserLoginResponse.create({
        sessionId: "testSynchronouslyLoadProtoFile",
        userPrivilege: "John123",
        isTokenType: false,
        formatTimestamp: "12342222"
    });
    let arrayBuffer = user.UserLoginResponse.encode(msg).finish();
    let expectedValue = {"sessionId":"testSynchronouslyLoadProtoFile","userPrivilege":"John123","isTokenType":false,"formatTimestamp":"12342222"};
    let decodeMsg;
    let startTime = Date.now();
    for (let i = 0; i < 100000; i++) {
        decodeMsg = user.UserLoginResponse.decode(arrayBuffer);
        // print("protobuf decode:" + JSON.stringify(decodeMsg));
    }
    let endTime = Date.now();
    if (!areEqual(JSON.stringify(decodeMsg), JSON.stringify(expectedValue))) {
        throw new Error('decode failed');
    }
    print(`protobuf_dencode: ${endTime - startTime} ms`);
}

//verify: null
function verify_Test() {
    let msg = user.UserLoginResponse.create({
        sessionId: "testSynchronouslyLoadProtoFile",
        userPrivilege: "John123",
        isTokenType: false,
        formatTimestamp: "12342222"
    });
    let result;
    let startTime = Date.now();
    for (let i = 0; i < 100000; i++) {
        result = user.UserLoginResponse.verify(msg);
        // print(result);
    }
    let endTime = Date.now();
    if (!areEqual(result, null)) {
        throw new Error('decode failed');
    }
    print(`protobuf_verify: ${endTime - startTime} ms`);
}


//fromObject(msg)
function fromObject_Test() {
    let msg = user.UserLoginResponse.create({
        sessionId: "testSynchronouslyLoadProtoFile",
        userPrivilege: 123,
        isTokenType: false,
        formatTimestamp: 12342222
    });
    let expectedValue = {"sessionId":"testSynchronouslyLoadProtoFile","userPrivilege":123,"isTokenType":false,"formatTimestamp":12342222};
    let result;
    let startTime = Date.now();
    for (let i = 0; i < 500000; i++) {
        result = user.UserLoginResponse.fromObject(msg);
        // print(JSON.stringify(result));
    }
    let endTime = Date.now();
    if (!areEqual(JSON.stringify(result), JSON.stringify(expectedValue))) {
        throw new Error('fromObject failed');
    }
    print(`protobuf_fromObject: ${endTime - startTime} ms`);
} 

//toObject(msg)
function toObject_Test() {
    let msg = user.UserLoginResponse.create({
        sessionId: "testSynchronouslyLoadProtoFile",
        userPrivilege: 123,
        isTokenType: false,
        formatTimestamp: 12342222
    });
    let expectedValue = {"sessionId":"testSynchronouslyLoadProtoFile","userPrivilege":123,"isTokenType":false,"formatTimestamp":12342222};
    let result;
    let startTime = Date.now();
    for (let i = 0; i < 500000; i++) {
        result = user.UserLoginResponse.toObject(msg);
        // print(JSON.stringify(result));
    }
    let endTime = Date.now();
    if (!areEqual(JSON.stringify(result), JSON.stringify(expectedValue))) {
        throw new Error('fromObject failed');
    }
    print(`protobuf_toObject: ${endTime - startTime} ms`);
} 

// Expected results and actual results verification 
function areEqual(expected, actual) {
    // If expected and actual are both NaN, consider them equal.
    if (expected === actual || expected !== expected && actual !== actual) {
        return true
    } else {
        return false
    }
  }

create_Test();
encode_Test();
encodeDelimited_Test();
decode_Test();
verify_Test();
fromObject_Test();
toObject_Test();
